package com.lv.common.utils;

import android.annotation.SuppressLint;
import android.util.Pair;

import com.blankj.utilcode.util.LogUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

/**
 * 时间日期帮助类
 */
public class TimeUtil {

    /**
     * 预约看房英文格式
     **/
    public final static DateFormat df_english = new SimpleDateFormat("yyyy MMM dd", Locale.UK);

    public static String getDate(String format) {
        SimpleDateFormat formatBuilder = new SimpleDateFormat(format, Locale.CHINA);
        return formatBuilder.format(new Date());
    }

    /**
     * 格式化日期  英文格式
     **/
    public static String formatTimeToUSA(Date date) {
        return df_english.format(date);
    }

    public static String getDate() {
        return getDate("yyyy-MM-dd HH:mm:ss");
    }

    public static String getTime() {
        return getDate("HH:mm");
    }

    private static void zeroFromHour(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    private static Calendar zeroFromHour(long milliseconds) {
        Calendar calendar = Calendar.getInstance(); // 获得一个日历
        calendar.setTimeInMillis(completMilliseconds(milliseconds));
        zeroFromHour(calendar);
        return calendar;
    }

    private static long completMilliseconds(long milliseconds) {
        String milStr = Long.toString(milliseconds);
        if (milStr.length() == 10) {
            milliseconds = milliseconds * 1000;
        }
        return milliseconds;
    }


    public static String getWhatDay(long timeStamp) {
        Calendar cal = zeroFromHour(timeStamp);
        String whatDay = "";
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            whatDay = "周六";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            whatDay = "周日";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            whatDay = "周一";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
            whatDay = "周二";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
            whatDay = "周三";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            whatDay = "周四";
        }
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            whatDay = "周五";
        }
        return whatDay;
    }

    /**
     * 获取需要的时间
     * yyyy-MM-dd
     **/
    public static String longNeedTime(long longTime) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    /**
     * 获取需要的时间
     * yyyy/MM/dd
     **/
    public static String longNeedTime_(long longTime) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    /**
     * 获取需要的时间
     **/
    public static String longNeedTimePoint(long longTime) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd", Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    /**
     * 获取需要的时间
     **/
    public static String longNeedTimes(long longTime) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    /**
     * 获取需要的时间
     **/
    public static String longNeedTimes2(long longTime) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    /**
     * 获取需要的时间
     **/
    public static String longNeedTimeTwo(long longTime) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm", Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    /**
     * 获取需要的时间
     **/
    public static String longNeedTimeByFormat(long longTime, String format) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    /**
     * 时间转换成分和秒
     **/
    public static String timeToMS(int second) {
        String result = null;
        try {
            Date date = new Date(second * 1000);

            SimpleDateFormat df = new SimpleDateFormat("mm:ss");
            df.setTimeZone(TimeZone.getTimeZone("GMT+0:00"));
            result = df.format(date);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }

    /*
     * 将时间戳转换为时间
     *
     * s就是时间戳
     */

    public static String stampToDate(long lt) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        //如果它本来就是long类型的,则不用写这一步
//        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间戳转换为时间
     *
     * s就是时间戳
     */

    public static String stampToDateS(String s, String fprmat) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fprmat);
        //如果它本来就是long类型的,则不用写这一步
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间转换为时间戳
     */
    @SuppressLint("SimpleDateFormat")
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    @SuppressLint("SimpleDateFormat")
    public static String dateToStampByFormat(String s, String format) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /**
     * 获取需要的时间
     **/
    public static String longNeedTimeMD(long longTime) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    /**
     * 获取需要的时间
     **/
    public static String longNeedTimeMD2(long longTime) {
        String time;
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd", Locale.CHINA);
        time = sdf.format(new Date(longTime));
        return time;
    }

    public static String getFavoriteCollectTime(long milliseconds) {
        String showDataString = "";
        Date today = new Date();
        Date date = new Date(milliseconds);
        Date firstDateThisYear = new Date(today.getYear(), 0, 0);
        if (!date.before(firstDateThisYear)) {
            SimpleDateFormat dateformatter = new SimpleDateFormat("MM-dd", Locale.getDefault());
            showDataString = dateformatter.format(date);
        } else {
            SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            showDataString = dateformatter.format(date);
        }
        return showDataString;
    }

    public static String getFavoriteCollectTime2(long milliseconds) {
        String showDataString = "";
        Date today = new Date();
        Date date = new Date(milliseconds);
        Date firstDateThisYear = new Date(today.getYear(), 0, 0);
        if (!date.before(firstDateThisYear)) {
            SimpleDateFormat dateformatter = new SimpleDateFormat("MM/dd", Locale.getDefault());
            showDataString = dateformatter.format(date);
        } else {
            SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
            showDataString = dateformatter.format(date);
        }
        return showDataString;
    }

    public static String getNowDatetime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return (formatter.format(new Date()));
    }

    public static String getNowDate() {
        return getDate("yyyy_MM_dd");
    }

    public static String getNowDate2() {
        return getDate("yyyy-MM-dd");
    }

    public static final int MIN_IN_MS = 60 * 1000;

    public static boolean isEarly(int days, long time) {
        return (currentTimeMillis() - time) > (days * 24 * 3600 * 1000);
    }

    public static long getNow_millisecond() {
        return (new Date()).getTime();
    }

    public static int currentTimeSecond() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    public static long currentTimeMillis() {
        return System.currentTimeMillis();
    }

    public static long[] getTsTimes() {
        long[] times = new long[2];

        Calendar calendar = Calendar.getInstance();

        times[0] = calendar.getTimeInMillis() / 1000;

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        times[1] = calendar.getTimeInMillis() / 1000;

        return times;
    }

    public static String getFormatDatetime(int year, int month, int day) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(new GregorianCalendar(year, month, day).getTime());
    }

    public static Date getDateFromFormatString(String formatDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(formatDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static int getNow() {
        return (int) ((new Date()).getTime() / 1000);
    }

    public static String getNowDateTime(String format) {
        Date date = new Date();

        SimpleDateFormat df = new SimpleDateFormat(format, Locale.getDefault());
        return df.format(date);
    }

    public static String getDateString(long milliseconds) {
        return getDateTimeString(milliseconds, "yyyyMMdd");
    }

    public static String getTimeString(long milliseconds) {
        return getDateTimeString(milliseconds, "HHmmss");
    }

    public static String getBeijingNowTimeString(String format) {
        TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai");

        Date date = new Date(currentTimeMillis());
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
        formatter.setTimeZone(timezone);

        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTimeZone(timezone);
        String prefix = gregorianCalendar.get(Calendar.AM_PM) == Calendar.AM ? "上午" : "下午";

        return prefix + formatter.format(date);
    }

    public static String getBeijingNowTime(String format) {
        TimeZone timezone = TimeZone.getTimeZone("Asia/Shanghai");

        Date date = new Date(currentTimeMillis());
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
        formatter.setTimeZone(timezone);

        return formatter.format(date);
    }

    public static String getDateTimeString(long milliseconds, String format) {
        Date date = new Date(milliseconds);
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault());
        return formatter.format(date);
    }


    public static String getTimeShowString(long milliseconds, boolean abbreviate) {
        String dataString;
        String timeStringBy24;
        Date currentTime = new Date(milliseconds);
        Date today = new Date();
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        Date todaybegin = todayStart.getTime();
        Date yesterdaybegin = new Date(todaybegin.getTime() - 3600 * 24 * 1000);
        Date preyesterday = new Date(yesterdaybegin.getTime() - 3600 * 24 * 1000);

        if (!currentTime.before(todaybegin)) {
            dataString = "今天";
        } else if (!currentTime.before(yesterdaybegin)) {
            dataString = "昨天";
        } else if (!currentTime.before(preyesterday)) {
            dataString = "前天";
        } else if (isSameWeekDates(currentTime, today)) {
            dataString = getWeekOfDate(currentTime);
        } else {
            SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            dataString = dateformatter.format(currentTime);

        }

        SimpleDateFormat timeformatter24 = new SimpleDateFormat("HH:mm", Locale.getDefault());
        timeStringBy24 = timeformatter24.format(currentTime);

        if (abbreviate) {
            if (!currentTime.before(todaybegin)) {
                return getTodayTimeBucket(currentTime);
            } else {
                return dataString;
            }
        } else {
            return dataString + " " + timeStringBy24;
        }
    }

    public static String getTimeShowStringChina(long milliseconds, boolean abbreviate) {
        String dataString;
        String timeStringBy24;
        Date currentTime = new Date(milliseconds);
        Date today = new Date();
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        Date todaybegin = todayStart.getTime();
        Date yesterdaybegin = new Date(todaybegin.getTime() - 3600 * 24 * 1000);
        Date preyesterday = new Date(yesterdaybegin.getTime() - 3600 * 24 * 1000);

        if (!currentTime.before(todaybegin)) {
            dataString = "今天";
        } else if (!currentTime.before(yesterdaybegin)) {
            dataString = "昨天";
        } else if (!currentTime.before(preyesterday)) {
            dataString = "前天";
        } else if (isSameWeekDates(currentTime, today)) {
            dataString = getWeekOfDate(currentTime);
        } else {
            SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
            dataString = dateformatter.format(currentTime);

        }

        SimpleDateFormat timeformatter24 = new SimpleDateFormat("HH:mm", Locale.getDefault());
        timeStringBy24 = timeformatter24.format(currentTime);

        if (abbreviate) {
            if (!currentTime.before(todaybegin)) {
                return getTodayTimeBucket(currentTime);
            } else {
                return dataString;
            }
        } else {
            return dataString + " " + timeStringBy24;
        }
    }

    /**
     * 根据不同时间段，显示不同时间
     *
     * @param date
     * @return
     */
    public static String getTodayTimeBucket(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        SimpleDateFormat timeformatter0to11 = new SimpleDateFormat("KK:mm", Locale.getDefault());
        SimpleDateFormat timeformatter1to12 = new SimpleDateFormat("hh:mm", Locale.getDefault());
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour >= 0 && hour < 5) {
            return "凌晨 " + timeformatter0to11.format(date);
        } else if (hour >= 5 && hour < 12) {
            return "上午 " + timeformatter0to11.format(date);
        } else if (hour >= 12 && hour < 18) {
            return "下午 " + timeformatter1to12.format(date);
        } else if (hour >= 18 && hour < 24) {
            return "晚上 " + timeformatter1to12.format(date);
        }
        return "";
    }

    /**
     * 根据日期获得星期
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDaysName = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        // String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysName[intWeek];
    }

    public static boolean isSameDay(long time1, long time2) {
        return isSameDay(new Date(time1), new Date(time2));
    }

    public static boolean isSameDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);

        boolean sameDay = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
        return sameDay;
    }

    /**
     * 判断两个日期是否在同一周
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameWeekDates(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (0 == subYear) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
            // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        }
        return false;
    }

    public static long getSecondsByMilliseconds(long milliseconds) {
        long seconds = new BigDecimal((float) ((float) milliseconds / (float) 1000)).setScale(0,
                BigDecimal.ROUND_HALF_UP).intValue();
        // if (seconds == 0) {
        // seconds = 1;
        // }
        return seconds;
    }

    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else retStr = "" + i;
        return retStr;
    }

    public static String getElapseTimeForShow(int milliseconds) {
        StringBuilder sb = new StringBuilder();
        int seconds = milliseconds / 1000;
        if (seconds < 1)
            seconds = 1;
        int hour = seconds / (60 * 60);
        if (hour != 0) {
            sb.append(hour).append("小时");
        }
        int minute = (seconds - 60 * 60 * hour) / 60;
        if (minute != 0) {
            sb.append(minute).append("分");
        }
        int second = (seconds - 60 * 60 * hour - 60 * minute);
        if (second != 0) {
            sb.append(second).append("秒");
        }
        return sb.toString();
    }

    public static boolean isSameDay(long millis1, long millis2, TimeZone timeZone) {
        long interval = millis1 - millis2;
        return interval < 86400000 && interval > -86400000 && millis2Days(millis1, timeZone) == millis2Days(millis2, timeZone);
    }

    private static long millis2Days(long millis, TimeZone timeZone) {
        return (((long) timeZone.getOffset(millis)) + millis) / 86400000;
    }


    /**
     * 获取日期当月的天数
     *
     * @param dateStr yyyy-MM 或者yyyy-MM-dd
     */
    public static int getDaysByDate(String dateStr) {
        int year = Integer.parseInt(dateStr.substring(0, 4));
        int month = Integer.parseInt(dateStr.substring(5, 7));
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0);
        return c.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 当月剩余天数
     *
     * @param date 格式yyyy-MM-dd
     *             Integer days = monthEndNum("2023-01-20");
     *             System.out.println("2023年1月剩余天数:"+days);
     */
    public static Integer monthEndNum(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(dateTime);
        int today = c.get(Calendar.DAY_OF_MONTH);
        int last = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        return last - today;
    }


    /**
     * 获取年
     *
     * @return
     */
    public static int getYear() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.YEAR);
    }

    /**
     * 获取月
     *
     * @return
     */
    public static int getMonth() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取日
     *
     * @return
     */
    public static int getDay() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.DATE);
    }

    /**
     * 获取时
     *
     * @return
     */
    public static int getHour() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.HOUR);
    }

    /**
     * 获取分
     *
     * @return
     */
    public static int getMinute() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.MINUTE);
    }

    //获取指定年月前几个月的时间
    public static Pair<Integer, Integer> getPreData(int year, int month, int subMonth) {
//        Calendar cd = Calendar.getInstance();
//        int year = cd.get(Calendar.YEAR);
//        int month = cd.get(Calendar.MONTH);
        int newMonth = month - subMonth;
        int newYear = year;
        if (newMonth < 0) {
            newMonth += 12;
            newYear--;
        }
        return new Pair(newYear, newMonth);
    }

    //判断日期是否有效
    public static boolean isDateExists(int year, int month, int day) {
        month = month - 1;//注意月份是从0开始的，即0代表1月
        // 创建一个日历实例
        Calendar calendar = Calendar.getInstance();
        // 设置日历的年月日
        calendar.set(year, month, day);
        // 获取设置后的年月日
        int yearSet = calendar.get(Calendar.YEAR);
        int monthSet = calendar.get(Calendar.MONTH);
        int daySet = calendar.get(Calendar.DAY_OF_MONTH);

        // 检查年月日是否与设置的相同，确保日期有效
        return yearSet == year && monthSet == month && daySet == day;
    }

    /**
     * 获得 今天的 年-月-日 星期X
     */
    public static String getToday(String formart) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formart, Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        //当前时间，貌似多余，其实是为了所有可能的系统一致
        calendar.setTimeInMillis(System.currentTimeMillis());
        System.out.println("当前时间:" + simpleDateFormat.format(calendar.getTime()));
        String mToday = myDateFormat2(simpleDateFormat.format(calendar.getTime()));
        return mToday;
    }

    /**
     * 获得 本周一 年-月-日
     */
    public static String getZhou1(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String date = myDateFormat2(simpleDateFormat.format(calendar.getTime()));
        return date;
    }

    /**
     * 获得 本周二 年-月-日
     */
    public static String getZhou2() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("y年M月d日 E", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
        String date = myDateFormat2(simpleDateFormat.format(calendar.getTime()));
        return date;
    }

    /**
     * 获得 本周三 年-月-日
     */
    public static String getZhou3() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("y年M月d日 E", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
        String date = myDateFormat2(simpleDateFormat.format(calendar.getTime()));
        return date;
    }

    /**
     * 获得 本周四 年-月-日
     */
    public static String getZhou4() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("y年M月d日 E", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
        String date = myDateFormat2(simpleDateFormat.format(calendar.getTime()));
        return date;
    }

    /**
     * 获得 本周五 年-月-日
     */
    public static String getZhou5() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("y年M月d日 E", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
        String date = myDateFormat2(simpleDateFormat.format(calendar.getTime()));
        return date;
    }

    /**
     * 获得 本周六 年-月-日
     */
    public static String getZhou6() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("y年M月d日 E", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        String date = myDateFormat2(simpleDateFormat.format(calendar.getTime()));
        return date;
    }

    /**
     * 获得 本周日 年-月-日
     */
    public static String getZhou7() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("y年M月d日 E", Locale.CHINA);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String date = myDateFormat2(simpleDateFormat.format(calendar.getTime()));
        return date;
    }

    /**
     * 日期格式化 年-月-日
     */
    private static String myDateFormat2(String format) {
        String[] format_all = format.split(" ");
        format_all[0] = format_all[0].replace("年", "-"); // 替换
        format_all[0] = format_all[0].replace("月", "-"); // 替换
        format_all[0] = format_all[0].replace("日", ""); // 替换
        String formatBack = format_all[0];
        return formatBack;
    }

    /**
     * 日期格式化 年-月-日
     * 获取本周X的日期
     */
    public static String getDayOfWeek(int day, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        switch (day) {
            case 1:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                break;
            case 2:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
                break;
            case 3:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
                break;
            case 4:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
                break;
            case 5:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
                break;
            case 6:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
                break;
            case 7:
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                break;

        }
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 指定日期前几天
     *
     * @param days
     * @param time
     * @param format
     * @return
     */
    public static String getPreviousDaysDate(int days, String time, String format) {
        String simTime = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            Date date = simpleDateFormat.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -days);
            simTime = simpleDateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return simTime;
    }

    /**
     * 指定日期后几天
     *
     * @param days
     * @param time
     * @param format
     * @return
     */
    public static String getBeforDaysDate(int days, String time, String format) {
        String simTime = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            Date date = simpleDateFormat.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, days);
            simTime = simpleDateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();

        }
        return simTime;
    }

    /**
     * 获取两指定日期间隔天数
     *
     * @param startDate
     * @param endDate
     * @return yyyy-MM-dd
     */
    public static long getDaysBetweenDates(String startDate, String endDate, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date start = sdf.parse(startDate);
            Date end = sdf.parse(endDate);
            long diffInMillies = Math.abs(end.getTime() - start.getTime());
            return TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取指定日期后x个月的日期
     *
     * @param specifiedDateString yyyy-MM-dd
     * @param num                 三个月 传 3
     * @return yyyy-MM-dd
     */
    public static String AddMonths(String specifiedDateString, int num) {

        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 解析指定日期字符串为 LocalDate 对象
        LocalDate specifiedDate = LocalDate.parse(specifiedDateString, formatter);

        // 加上三个月
        LocalDate futureDate = specifiedDate.plusMonths(num);

        // 格式化结果为字符串（可选）
        String formattedFutureDate = futureDate.format(formatter);
//        LogUtils.e("指定日期和时间: " + specifiedDateString);
        LogUtils.e(num + "个月后的日期和时间: " + formattedFutureDate);
        return formattedFutureDate;
    }
}